msg() {
    ALL_OFF="\e[1;0m"
    BOLD="\e[1;1m"
    GREEN="${BOLD}\e[1;32m"
	local mesg=$1; shift
	printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}" "$@" >&2
}

add_users() {
    _l="/etc/login.defs"
    _p="/etc/passwd"
 
    ## get mini UID limit ##
    l=$(grep "^UID_MIN" $_l)
 
    ## get max UID limit ##
    l1=$(grep "^UID_MAX" $_l)
 
    ## use awk to print if UID >= $MIN and UID <= $MAX and shell is not /sbin/nologin   ##
    users=$(awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max  && $7 != "/sbin/nologin" ) print $0 }' "$_p" | cut -d: -f1)
    for i in $users; do
        msg "Adding user '$i' to user-group 'sambashare'"
        usermod -a -G sambashare $i
    done
}

add_group() {
    getent group "sambashare" &>/dev/null || groupadd -g 617 sambashare
    add_users
    chgrp sambashare var/lib/samba/usershare
}

post_upgrade() {
  add_group
  echo ""
  msg "Please add new users to 'sambashare' group."
  echo ""
}

post_install() {
  pacman -Qq samba-openrc &> /tmp/cmd1
  msg "Attempting to enable Samba services..."
  if [ "$(grep 'samba-openrc' /tmp/cmd1)" == "samba-openrc" ]; then
    rc-update add smb default
  else
    systemctl enable smb.service nmb.service
  fi
  add_group
  echo ""
  msg "Samba services should now be enabled."
  msg "Please add new users to 'sambashare' group."
  msg "Reboot your system, so changes will take affect."
  echo ""
}

post_remove() {
  pacman -Qq samba-openrc &> /tmp/cmd1
  msg "Attempting to disable Samba services..."
  if [ "$(grep 'samba-openrc' /tmp/cmd1)" == "samba-openrc" ]; then
    rc-update del smb default
  else
    systemctl disable smb.service nmb.service
  fi
  echo ""
  msg "Samba services should now be disabled. Reboot your system."
  echo ""
}

